const mysql = require('mysql2/promise');
const fs = require('fs');
const path = require('path');

async function setupAchievements() {
    let connection;
    try {
        // 创建数据库连接
        connection = await mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: 'Lycc.2025',
            database: 'fun_english_db',
            multipleStatements: true
        });

        console.log('✅ 数据库连接成功');

        // 读取SQL文件
        const sqlFile = path.join(__dirname, 'create-achievements-table.sql');
        const sql = fs.readFileSync(sqlFile, 'utf8');

        console.log('📝 执行SQL脚本...');
        
        // 执行SQL
        await connection.query(sql);

        console.log('✅ 成就表创建成功！');
        
        // 查询成就数量
        const [result] = await connection.execute('SELECT COUNT(*) as count FROM achievements');
        console.log(`📊 当前成就数量: ${result[0].count}`);

    } catch (error) {
        console.error('❌ 设置失败:', error);
        process.exit(1);
    } finally {
        if (connection) {
            await connection.end();
            console.log('👋 数据库连接已关闭');
        }
    }
}

setupAchievements();

